/**
 * @(#) DomainEntity.java
 */

package cn.edu.sjtu.se.dslab.component.data;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "domain_entity")
@Inheritance(strategy = InheritanceType.JOINED)
public class DomainEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	private long id;

	private Set<DomainEntity> depends;
	private Set<DomainEntity> childList;

	/**
	 * 英文名称
	 */
	private String name;
	
	/**
	 * 中文名称
	 */
	private String cnName;

	private String type;

	private String uri;

	private String description;
	
	private EntityDiagram diagram;

	private Set<DataAttribute> attributes;

	private Set<Operation> opertions;

	private Set<DomainEntity> aggregations;

	private Set<DomainEntity> extendsSet;
	
	private boolean deleted;
	
	@Column(name = "deleted", nullable = true)
	public boolean isDeleted() {
		return deleted;
	}
	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}
	
	public DomainEntity() {
		super();
		this.deleted = false;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "entity", fetch = FetchType.EAGER)
	public Set<DataAttribute> getAttributes() {
		return attributes;
	}

	public void setAttributes(Set<DataAttribute> attributes) {
		this.attributes = attributes;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "entity", fetch = FetchType.EAGER)
	public Set<Operation> getOpertions() {
		return opertions;
	}

	public void setOpertions(Set<Operation> opertions) {
		this.opertions = opertions;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	
	@Column(name = "description", nullable = true, length = 255)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	// 多对多处理
	@ManyToMany(targetEntity = cn.edu.sjtu.se.dslab.component.data.DomainEntity.class, cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinTable(name = "domain_dependence", joinColumns = @JoinColumn(name = "child_id"), inverseJoinColumns = @JoinColumn(name = "parent_id"))
	public Set<DomainEntity> getDepends() {
		return depends;
	}

	public void setDepends(Set<DomainEntity> depends) {
		this.depends = depends;
	}

	@ManyToMany(cascade = { CascadeType.ALL }, mappedBy = "depends", targetEntity = cn.edu.sjtu.se.dslab.component.data.DomainEntity.class, fetch = FetchType.EAGER)
	public Set<DomainEntity> getChildList() {
		return childList;
	}

	public void setChildList(Set<DomainEntity> childList) {
		this.childList = childList;
	}

	@Column(name = "name", nullable = false, length = 32)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	@Column(name = "cn_name", nullable = true, length = 32)
	public String getCnName() {
		return cnName;
	}

	public void setCnName(String cnName) {
		this.cnName = cnName;
	}

	@Column(name = "type", nullable = false, length = 255)
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Column(name = "uri", nullable = true, length = 255)
	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DomainEntity other = (DomainEntity) obj;
		if (childList == null) {
			if (other.childList != null)
				return false;
		} else if (!childList.equals(other.childList))
			return false;
		if (depends == null) {
			if (other.depends != null)
				return false;
		} else if (!depends.equals(other.depends))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		return true;
	}

	@ManyToMany(targetEntity = cn.edu.sjtu.se.dslab.component.data.DomainEntity.class, cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinTable(name = "domain_aggregation", joinColumns = @JoinColumn(name = "child_id"), inverseJoinColumns = @JoinColumn(name = "parent_id"))
	public Set<DomainEntity> getAggregations() {
		return aggregations;
	}

	public void setAggregations(Set<DomainEntity> aggregations) {
		this.aggregations = aggregations;
	}

	@ManyToMany(targetEntity = cn.edu.sjtu.se.dslab.component.data.DomainEntity.class, cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinTable(name = "domain_extends", joinColumns = @JoinColumn(name = "child_id"), inverseJoinColumns = @JoinColumn(name = "parent_id"))
	public Set<DomainEntity> getExtendsSet() {
		return extendsSet;
	}

	public void setExtendsSet(Set<DomainEntity> extendsSet) {
		this.extendsSet = extendsSet;
	}

	@ManyToOne(cascade = { CascadeType.ALL })
	@JoinColumn(name = "diagram_id")
	public EntityDiagram getDiagram() {
		return diagram;
	}

	public void setDiagram(EntityDiagram diagram) {
		this.diagram = diagram;
	}

}
